<!-- 商家销量函数的组件 -->
<template>
  <div class="com-container">
    <div class="com-chart" ref="seller_ref"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { sellerData } from "@/api/index";
export default {
  name: "Seller",
  data() {
    return {
      chartInstance: null,
      allData: [], //服务器返回的数据
      currentPage: 1, //当前显示的页数
      totalPage: 0, //一共多少页
      timerId: null, //定时器
    };
  },
  mounted() {
    this.initChart();
    this.getData();
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter(); //初始加载时调用一次
  },
  methods: {
    //初始化echartInstance对象
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.seller_ref, this.theme);
      //对图标初始化配置的控制
      const initOption = {
        title: {
          text: "▎商家销售统计",
          left: 20,
          top: 20,
        },
        grid: {
          top: "20%",
          left: "3%",
          right: "6%",
          bottom: "3%",
          containLabel: true, //距离是包含坐标轴的文字
        },
        xAxis: {
          type: "value",
        },
        yAxis: {
          type: "category",
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "line",
            z: 0,
            lineStyle: {
              color: "#2D3443",
            },
          },
        },
        series: [
          {
            type: "bar",
            label: {
              //文字
              show: true,
              position: "right",
              textStyle: {
                color: "white",
              },
            },
            itemStyle: {
              //指明颜色渐变方向
              //指明不同百分比之下的颜色的值
              color: new this.$echarts.graphic.LinearGradient(0, 0, 1, 0, [
                {
                  offset: 0,
                  color: "#5052EE",
                },
                {
                  offset: 1,
                  color: "#AB6EE5",
                },
              ]),
            },
          },
        ],
      };
      this.chartInstance.setOption(initOption);
      //对图标对象进行鼠标事件监听
      this.chartInstance.on("mouseover", () => {
        clearInterval(this.timerId);
      });
      this.chartInstance.on("mouseout", () => {
        this.startInterval();
      });
    },
    //获取服务器的数据
    async getData() {
      try {
        this.allData = await sellerData();
        //数据排序
        this.allData.sort((a, b) => {
          return a.value - b.value;
        });
        //每五个元素显示一页
        this.totalPage =
          this.allData.length % 5 === 0
            ? this.allData.length / 5
            : this.allData.length / 5 + 1;
        this.updateChart();
        //启动定时器
        this.startInterval();
      } catch (e) {
        console.log(e);
      }
    },
    //更新图表
    updateChart() {
      const start = (this.currentPage - 1) * 5;
      const end = this.currentPage * 5;
      const showData = this.allData.slice(start, end);
      const sellerNames = showData.map((item) => item.name);
      const sellerValues = showData.map((item) => item.value);
      //数据配置项
      const dataOption = {
        yAxis: {
          data: sellerNames,
        },
        series: [
          {
            data: sellerValues,
          },
        ],
      };
      this.chartInstance.setOption(dataOption);
    },
    //定时器
    startInterval() {
      if (this.timerId) clearInterval(this.timerId);
      this.timerId = setInterval(() => {
        this.currentPage++;
        if (this.currentPage > this.totalPage) {
          this.currentPage = 1;
        }
        this.updateChart();
      }, 3000);
    },
    //屏幕适配,浏览器大小变化时调用的方法
    screenAdapter() {
      // console.log(this.$refs.seller_ref.offsetWidth);
      //标题大小
      const titleFontSize = (this.$refs.seller_ref.offsetWidth / 100) * 3.6;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
        tooltip: {
          axisPointer: {
            lineStyle: {
              width: titleFontSize,
            },
          },
        },
        series: [
          {
            barWidth: titleFontSize, //宽度
            itemStyle: {
              barBorderRadius: [0, titleFontSize / 2, titleFontSize / 2, 0],
            },
          },
        ],
      };
      this.chartInstance.setOption(adapterOption);
      //手动调用resize()
      this.chartInstance.resize();
    },
  },
  //销毁之前关闭定时器
  beforeDestroy() {
    if (this.timerId) clearInterval(this.timerId);
    window.removeEventListener("resize", this.screenAdapter);
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme() {
      this.chartInstance.dispose()//销毁当前的图标
      this.initChart()//重新初始化图标
      this.screenAdapter()//完成屏幕适配
      this.updateChart()//更新图标展示
    }
  }
};
</script>

<style lang="less" scoped>

</style>
